/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.passport.service.plugins  
 */
package com.apache.passport.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.passport.common.DesUtils;
import com.apache.passport.entity.Token;

/**
 * description:  用户退出
 * @author Hou Dayu 创建时间：2014-12-18  
 * @Copyright(c)2014:
 */
public class LogoutUserPlugin implements PluginConnector {

	/**
	 * TODO 根据cookie中的value,删除缓存中的key
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo paramsvo) throws Exception {
		ResultEntity result = new ResultEntity();
		result.setResult("true");
		String tokenVal = (String) paramsvo.getParams("tokenId");
		String userEname = (String) paramsvo.getParams("userEname");
		if (Validator.isNull(tokenVal)) {
			result.setMessage("缺少必要参数:[tokenId]");
			result.setEntity("F");
			return result;
		}
		Token token = (Token) SystemTools.getInstance().getCache("loginToken").getCacheCloneByKey(tokenVal);
		if (Validator.isEmpty(token)) {
			result.setMessage("证书已失效或不存在");
			result.setEntity("N");
			return result;
		}
		if (Validator.isNull(userEname)) {
			result.setMessage("缺少必要参数:[userEname]");
			result.setEntity("F");
			return result;
		}
		String userEnameEn = DesUtils.getInstance().decrypt(token.getUserEname());
		if (userEname.equals(userEnameEn)) {
			SystemTools.getInstance().getCache("loginToken").removeCacheObject(tokenVal);
			result.setMessage("用户退出成功");
			result.setEntity("T");
			return result;
		} else {
			result.setMessage("用户名不匹配");
			result.setEntity("F");
			return result;
		}
	}
}
